package com.zhisheng.web17.demos.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhisheng.web17.demos.aop.MyBj;
import com.zhisheng.web17.demos.mapper.UserMapper;
import com.zhisheng.web17.demos.pojo.User;
import com.zhisheng.web17.demos.resp.MyResponseResult;
import com.zhisheng.web17.demos.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建者： 智晟全哥
 */

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    @MyBj
    public List<User> list() {
        System.out.println("查询用户列表的方法...");
        List<User> list = new ArrayList<>();
        list.add(new User());
        return list;
    }

    @Override
    @MyBj
    public int insertUser(User user) {
        System.out.println("添加了用户:"+user);
        return 1;
    }

    @Override
    public MyResponseResult<Integer> insertUser2(User user) {
        int i = userMapper.insertUser(user);
        return i>=1?MyResponseResult.ok(1):MyResponseResult.error("添加用户失败!");
    }

    @Override
    public MyResponseResult<PageInfo<User>> listUserByPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Page<User> list = (Page<User>) userMapper.selectAll();
        PageInfo<User> info = new PageInfo<>(list);
        return MyResponseResult.ok(info);
    }

    @Override
    public MyResponseResult<Integer> delById(Integer id) {
        int i = userMapper.delById(id);
        return i>=1?MyResponseResult.ok(1):MyResponseResult.error("删除用户失败!");
    }
}
